package com.atguigu.tingshu.handler;

import com.atguigu.tingshu.entity.CDCEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * @author atguigu-mqx
 * @ClassName AlbumInfoCdcHandler
 * @description: TODO
 * @date 2025年03月05日
 * @version: 1.0
 */
@Component
@Slf4j
@CanalTable("album_info")
public class AlbumInfoCdcHandler implements EntryHandler<CDCEntity> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void insert(CDCEntity cdcEntity) {
        EntryHandler.super.insert(cdcEntity);
    }

    @Override
    public void update(CDCEntity before, CDCEntity after) {
        log.info("更新了专辑信息before：{}",before.getId());
        log.info("更新了专辑信息after：{}",after.getId());
        //  一种写入： 一种：直接删除,查询的时候再写 album:info:[5]
        try {
            String key = "album:info:["+after.getId()+"]";
            redisTemplate.delete(key);
        } catch (Exception e) {
            log.error("更新专辑信息失败：{}",e.getMessage());
            //  sendMsg(exchange,routingkey,after.getId()); 专辑微服务监听;
            System.out.println(e.getMessage());
        }
        EntryHandler.super.update(before, after);
    }

    @Override
    public void delete(CDCEntity cdcEntity) {
        EntryHandler.super.delete(cdcEntity);
    }
}
